package org.xzp04;

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {
	private Queue<PetEnterQueue> dogQ;
	private Queue<PetEnterQueue> catQ;
	private int count;

	public DogCatQueue() {
		this.dogQ = new LinkedList<>();
		this.catQ = new LinkedList<>();
		this.count = 0;
	}

	public void add(Pet pet) {
		if (pet.getPetType().equals("dog")) {
			dogQ.add(new PetEnterQueue(pet, this.count++));
		} else if (pet.getPetType().equals("cat")) {
			catQ.add(new PetEnterQueue(pet, this.count++));
		} else {
			throw new RuntimeException("your pet is not dog or cat");
		}
	}

	public Dog pollDog() {
		if (dogQ.isEmpty()) {
			throw new RuntimeException("no dog is here!");
		} else {
			return (Dog) dogQ.poll().getPet();
		}
	}

	public Cat pollCat() {
		if (catQ.isEmpty()) {
			throw new RuntimeException("no dog is here!");
		} else {
			return (Cat) catQ.poll().getPet();
		}
	}

	public Pet pollAll() {
		if (!dogQ.isEmpty() && !catQ.isEmpty()) {
			if (dogQ.peek().getCount() < catQ.peek().getCount()) {
				return dogQ.poll().getPet();
			} else {
				return catQ.poll().getPet();
			}
		} else if (!dogQ.isEmpty()) {
			return dogQ.poll().getPet();
		} else if (!catQ.isEmpty()) {
			return catQ.poll().getPet();
		} else {
			throw new RuntimeException("your queue is empty!");
		}
	}
}
